import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'
import Login from '../views/Login.vue'

import ArticleEdit from '../views/ArticleEdit.vue'
import ArticleList from '../views/ArticleList.vue'

import AdminUserEdit from '../views/AdminUserEdit.vue'
import AdminUserList from '../views/AdminUserList.vue'

import AboutEdit from '../views/AboutEdit.vue'
import AboutList from '../views/AboutList.vue'

import FriendshipEdit from '../views/FriendshipEdit.vue'
import FriendshipList from '../views/FriendshipList.vue'

import TimelineEdit from '../views/TimelineEdit.vue'
import TimelineList from '../views/TimelineList.vue'

import MessageEdit from '../views/MessageEdit.vue'
import MessageList from '../views/MessageList.vue'

import TagEdit from '../views/TagEdit.vue'
import TagList from '../views/TagList.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/main',
    component: Main,
    children: [
      { path: '/articles/create', component: ArticleEdit },
      { path: '/articles/edit/:id', component: ArticleEdit, props: true },
      { path: '/articles/list', component: ArticleList },

      { path: '/admin_users/create', component: AdminUserEdit },
      { path: '/admin_users/edit/:id', component: AdminUserEdit, props: true },
      { path: '/admin_users/list', component: AdminUserList },

      { path: '/about/create', component: AboutEdit },
      { path: '/about/edit/:id', component: AboutEdit, props: true },
      { path: '/about/list', component: AboutList },

      { path: '/friendships/create', component: FriendshipEdit },
      { path: '/friendships/edit/:id', component: FriendshipEdit, props: true },
      { path: '/friendships/list', component: FriendshipList },

      { path: '/timelines/create', component: TimelineEdit },
      { path: '/timelines/edit/:id', component: TimelineEdit, props: true },
      { path: '/timelines/list', component: TimelineList },

      { path: '/leaveMessages/create', component: MessageEdit },
      { path: '/leaveMessages/edit/:id', component: MessageEdit, props: true },
      { path: '/leaveMessages/list', component: MessageList },

      { path: '/tags/create', component: TagEdit },
      { path: '/tags/edit/:id', component: TagEdit, props: true },
      { path: '/tags/list', component: TagList },

    ]
  },

]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from从哪个路径跳转而来
  // next是一个函数  表示放行
  if (to.path === '/login') return next();
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) return next('/login');
  next();
})

export default router
